<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width:200px;margin-bottom:5px"
        v-model="search.searchName"
        placeholder="请输入供应商名字"
        clearable
      ></el-input>
      <el-input
        style="width:200px;margin-bottom:5px"
        v-model="search.searchCode"
        placeholder="请输入订单编号"
        clearable
      ></el-input>
      <el-date-picker
        v-model="search.searchTime"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-select v-model="search.status" placeholder="请选择状态">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="search.is_storage" placeholder="请选择入库单状态">
        <el-option
          v-for="item in optionsType"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
      >查询</el-button>
      <el-button
        class="add"
        size="small"
        type="primary"
        icon="el-icon-circle-plus"
        @click="addShopping"
      >添加购货单</el-button>
    </div>
    <el-table max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center"></el-table-column>
      <el-table-column prop="id" label="订单号" align="center"></el-table-column>
      <el-table-column prop="supplier.name" label="供应商" align="center"></el-table-column>
      <el-table-column prop="statusMsg" label="状态" align="center"></el-table-column>
      <el-table-column prop="is_storageMsg" label="入库单状态" align="center"></el-table-column>

      <el-table-column prop="create_at" label="时间" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="360px">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="mini"
            v-if="scope.row.status == 1"
            icon="el-icon-delete"
            @click="deletes(scope)"
          >删除</el-button>
          <el-button
            type="primary"
            size="mini"
            v-if="scope.row.status == 1"
            icon="el-icon-edit"
            @click="modification(scope)"
          >编辑</el-button>
          <el-button type="info" size="mini" icon="el-icon-info" @click=" particulars(scope)">详情</el-button>
          <el-button
            type="warning"
            size="mini"
            v-if="scope.row.status == 1"
            icon="el-icon-s-promotion"
            @click="handleEdit(scope)"
          >提交</el-button>
          <el-button
            size="mini"
            v-if="scope.row.status == 2"
            icon="el-icon-edit-outline"
            @click="confirm(scope)"
          >审核</el-button>
          <el-button
            type="primary"
            size="mini"
            v-if="scope.row.status == 3"
            icon="el-icon-truck"
            @click="send(scope)"
          >发货</el-button>
          <el-button
            type="success"
            size="mini"
            v-if="scope.row.status == 5"
            icon="el-icon-circle-check"
            @click="complete(scope)"
          >确认</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination @page="handleSizeChange" @pagesize="handleCurrentChange" :pageData="pageData"></pagination>
    <!-- addDialog -->
    <addShoppingDialog @upData="getData" :dialog="dialog"></addShoppingDialog>
    <!-- 详情Dailog -->
    <particularsDialog :particularsDialogs="particularsDialogs"></particularsDialog>
    <!-- 编辑Dailog -->
    <compileShoppingDialog :compileDialog="compileDialog"></compileShoppingDialog>
  </div>
</template>
<script>
import CompileShoppingDialog from "@/components/shopping/compileShoppingDialog";
import AddShoppingDialog from "@/components/shopping/addShoppingDialog";
import ParticularsDialog from "@/components/shopping/particularsDialog";
import { parseTime } from "@/utils/index";
import Pagination from "@/components/pagination";
import {
  shoppingList,
  deletes,
  submit,
  confirm,
  send,
  complete
} from "@/api/shopping/shoppingList";
export default {
  components: {
    pagination: Pagination,
    addShoppingDialog: AddShoppingDialog,
    particularsDialog: ParticularsDialog,
    compileShoppingDialog: CompileShoppingDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      dialog: {
        centerDialogVisible: false,
        dialogType: "",
        id: ""
      },
      particularsDialogs: {
        centerDialogVisible: false,
        id: ""
      },
      compileDialog: {
        centerDialogVisible: false,
        id: ""
      },
      search: {
        searchCode: "",
        searchName: "",
        status: "",
        searchTime: "",
        is_storage: ""
      },
      options: [
        {
          value: "",
          label: "全部"
        },
        {
          value: 1,
          label: "默认"
        },
        {
          value: 2,
          label: "已提交"
        },
        {
          value: 3,
          label: "已确认"
        },
        {
          value: 4,
          label: "已发货"
        },
        {
          value: 5,
          label: "已收货"
        },
        {
          value: 6,
          label: "已完成"
        }
      ],
      optionsType: [
        {
          value: "",
          label: "全部"
        },
        {
          value: 1,
          label: "未生成入库单"
        },
        {
          value: 2,
          label: "已生成入库单"
        }
      ],
      multipleSelection: []
    };
  },

  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        buy_order_id: this.search.searchCode,
        start_time: this.search.searchTime[0],
        end_time: this.search.searchTime[1],
        supplier_key: this.search.searchName,
        status: this.search.status,
        is_storage: this.search.is_storage
      };
      // return
      shoppingList(data)
        .then(res => {
          if (res.code == 0) {
            res.data.data.forEach(data => {
              switch (data.status) {
                case 0:
                  data.statusMsg = "删除";
                  break;
                case 1:
                  data.statusMsg = "默认";
                  break;
                case 2:
                  data.statusMsg = "已提交";
                  break;
                case 3:
                  data.statusMsg = "已确认";
                  break;
                case 4:
                  data.statusMsg = "已发货";
                  break;
                case 5:
                  data.statusMsg = "已收货";
                  break;
                case 6:
                  data.statusMsg = "已完成";
                  break;
              }
              switch (data.is_storage) {
                case 1:
                  data.is_storageMsg = "未生成入库单";
                  break;
                case 2:
                  data.is_storageMsg = "已生成入库单";
                  break;
              }
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //提交购货单
    handleEdit(scope) {
      let data = {
        buy_order_id: scope.row.id
      };
      this.$confirm("是否提交该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          if (scope.row.status == 1) {
            let data = {
              buy_order_id: scope.row.id
            };
            submit(data)
              .then(res => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: res.msg
                  });
                  this.getData();
                }
              })
              .catch(err => {
                console.log(err);
              });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消提交"
          });
        });
    },
    //审核购货单
    confirm(scope) {
      let data = {
        buy_order_id: scope.row.id
      };
      this.$confirm("是否提交该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          if (scope.row.status == 2) {
            let data = {
              buy_order_id: scope.row.id
            };
            confirm(data)
              .then(res => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: res.msg
                  });
                  this.getData();
                }
              })
              .catch(err => {
                console.log(err);
              });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消提交"
          });
        });
    },
    //发货
    send(scope) {
      let data = {
        buy_order_id: scope.row.id
      };
      this.$confirm("是否提交该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          if (scope.row.status == 3) {
            let data = {
              buy_order_id: scope.row.id
            };
            send(data)
              .then(res => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: res.msg
                  });
                  this.getData();
                }
              })
              .catch(err => {
                console.log(err);
              });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消提交"
          });
        });
    },
    //确认

    complete(scope) {
      let data = {
        buy_order_id: scope.row.id
      };
      this.$confirm("是否提交该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          if (scope.row.status == 5) {
            let data = {
              buy_order_id: scope.row.id
            };
            complete(data)
              .then(res => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: res.msg
                  });
                  this.getData();
                }
              })
              .catch(err => {
                console.log(err);
              });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消提交"
          });
        });
    },
    //添加商品
    addShopping() {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "";
      this.dialog.id = "";
    },
    //修改
    modification(scope) {
      this.compileDialog.centerDialogVisible = true;
      this.compileDialog.id = scope.row.id;
    },
    //详情
    particulars(scope) {
      this.particularsDialogs.centerDialogVisible = true;
      this.particularsDialogs.id = scope.row.id;
    },
    // 搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //删除
    deletes(scope) {
      this.$confirm("是否删除该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          if (scope.row.status == 1) {
            let data = {
              buy_order_id: scope.row.id
            };
            deletes(data)
              .then(res => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: res.msg
                  });
                  this.getData();
                }
              })
              .catch(err => {
                console.log(err);
              });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = [];
      let valMain = val;
      valMain.forEach(res => {
        this.multipleSelection.push(res.id);
      });
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>